package com.zhiyou.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alipay.api.AlipayApiException;

import com.zhiyou.service.AliPayService;
import com.zhiyou.utils.JsonResult;

@Controller
public class PayController {

	@Autowired
	AliPayService aliPayService;
	
	@RequestMapping("pay")
	public void pay(String WIDtotal_amount,Integer teacher_id, HttpServletRequest request, HttpServletResponse response) throws IOException{
		/*int user_id = 1;// 你应该是从session中获取
*/		System.out.println(WIDtotal_amount);
		String result = "";
		try {
			result = aliPayService.pay(teacher_id,WIDtotal_amount);
		} catch (AlipayApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response.setContentType("text/html;charset=utf-8");
		// 把表单写到页面中
		response.getWriter().write(result);
		// 刷新
		response.getWriter().flush();
		// 关闭
		response.getWriter().close();
	}
	@RequestMapping("update")
	public JsonResult pay(Integer teacher_id,HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, String[]> requestParams = request.getParameterMap();
		try {			
			Boolean boolean1 = aliPayService.updateOrder(requestParams,teacher_id);
			return new JsonResult("支付成功");
		} catch (Exception e) {
			return new JsonResult(500,"支付失败");
		}
	}
	
}
